//
//  NSObject+Hear.m
//  Hear
//
//  Created by mac on 2018/4/26.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "NSObject+Hear.h"

@implementation NSObject (Hear)

- (UIImage *)getImageWithName:(NSString *)imgname {
    UIImage *image = [UIImage imageNamed:imgname];
    return image;
}

- (NSURL *)returnUrlWithString:(NSString *)urlStr {
    NSURL *url = [NSURL URLWithString:urlStr];
    return url;
}

- (void)showMessage:(NSString *)message atPosition:(ShowPosition)position {
    [self showMessage:message atPosition:position duration:2];
}

- (void)showMessage:(NSString *)message atPosition:(ShowPosition)position duration:(NSInteger)time{
    CGSize screenSize = [[UIScreen mainScreen] bounds].size;
    
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    UIView *showview =  [[UIView alloc] init];
    showview.backgroundColor = [UIColor grayColor];
    showview.frame = CGRectMake(1, 1, 1, 1);
    showview.alpha = 1.0f;
    showview.layer.cornerRadius = 5.0f;
    showview.layer.masksToBounds = YES;
    [window addSubview:showview];
    
    UILabel *label = [[UILabel alloc] init];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:12.f],
                                 NSParagraphStyleAttributeName:paragraphStyle.copy};
    
    CGSize labelSize = [message boundingRectWithSize:CGSizeMake(207, 999)
                                             options:NSStringDrawingUsesLineFragmentOrigin
                                          attributes:attributes context:nil].size;
    
    label.frame = CGRectMake(10, 5, labelSize.width +20, labelSize.height);
    label.text = message;
    label.textColor = [UIColor whiteColor];
    label.textAlignment = 1;
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont boldSystemFontOfSize:12];
    [showview addSubview:label];
    
    switch (position) {
        case ShowPosition_Center:
            showview.frame = CGRectMake((screenSize.width - labelSize.width - 20)/2, screenSize.height/2, labelSize.width + 40, labelSize.height+10);
            break;
        case ShowPosition_Bottom:
            showview.frame = CGRectMake((screenSize.width - labelSize.width - 20)/2, screenSize.height-44, labelSize.width + 40, labelSize.height+10);
            break;
        default:
            break;
    }
    
    [UIView animateWithDuration:time animations:^{
        showview.alpha = 0;
    } completion:^(BOOL finished) {
        [showview removeFromSuperview];
    }];
    
}

- (BOOL)validatePhoneNum:(NSString *)phoneNum {
    NSString *phoneNumRegex = @"^(0|86|17951)?(13[0-9]|15[012356789]|17[03678]|18[0-9]|14[57])[0-9]{8}$";
    NSPredicate *phoneNumPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneNumRegex];
    return [phoneNumPredicate evaluateWithObject:phoneNum];
}

@end
